package com.lin.activity;


import com.lin.utils.DatabaseHelperForChattingMap;
import com.yunfeng.supercall.R;

import android.app.Activity;  
import android.content.ContentValues;  
import android.database.Cursor;  
import android.database.sqlite.SQLiteDatabase;  
import android.os.Bundle;  
import android.util.Log;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  

public class TestDatabasePOIActivity3 extends Activity implements OnClickListener{  
	private final static String SWORD="SWORD";  
	//声明五个控件对象  
	Button createDatabase=null;  
	Button updateDatabase=null;  
	Button insert=null;  
	Button update=null;  
	Button query=null;  
	Button delete=null;  

	public void onCreate(Bundle savedInstanceState) {  
		super.onCreate(savedInstanceState);  
		setContentView(R.layout.main_database);  
		findViews();  
	}  

	private void findViews() {  
		//根据控件ID得到控件  
		createDatabase = (Button) this.findViewById(R.id.createDatabase);  
		updateDatabase = (Button) this.findViewById(R.id.updateDatabase);  
		insert = (Button) this.findViewById(R.id.insert);  
		update = (Button) this.findViewById(R.id.update);  
		query = (Button) this.findViewById(R.id.query);  
		delete = (Button) this.findViewById(R.id.delete);  
		//添加监听器  
		createDatabase.setOnClickListener(this);  
		updateDatabase.setOnClickListener(this);  
		insert.setOnClickListener(this);  
		update.setOnClickListener(this);  
		query.setOnClickListener(this);  
		delete.setOnClickListener(this);  
	}  

	@Override  
	public void onClick(View v) {  
		//判断所触发的被监听控件，并执行命令  
		switch(v.getId()){  
		//创建数据库  
		case R.id.createDatabase:  
			//创建一个DatabaseHelper对象  
			DatabaseHelperForChattingMap dbHelper1 = new DatabaseHelperForChattingMap(TestDatabasePOIActivity3.this, "db");  
			//取得一个只读的数据库对象  
			SQLiteDatabase db1 = dbHelper1.getReadableDatabase();  
			break;  
			//更新数据库  
		case R.id.updateDatabase:  
			DatabaseHelperForChattingMap dbHelper2 = new DatabaseHelperForChattingMap(TestDatabasePOIActivity3.this, "db", 2);  
			SQLiteDatabase db2 = dbHelper2.getReadableDatabase();  
			break;  
			//插入数据  
		case R.id.insert:  
			//创建存放数据的ContentValues对象  
			ContentValues values = new ContentValues();  
			//像ContentValues中存放数据  
			values.put("username","username");
			values.put("avatar","avatar");
			values.put("uid","uids");
			DatabaseHelperForChattingMap dbHelper3 = new DatabaseHelperForChattingMap(TestDatabasePOIActivity3.this, "db", 2);  
			SQLiteDatabase db3 = dbHelper3.getWritableDatabase();  
			//数据库执行插入命令  
			long i = db3.insert("SuperFriendsBean", null, values);  
			break;  
			//更新数据信息  
		case R.id.update:  
			/*DatabaseHelperForChattingMap dbHelper4 = new DatabaseHelperForChattingMap(TestDatabasePOIActivity.this, "db", 2);  
			SQLiteDatabase db4 = dbHelper4.getWritableDatabase();  
			ContentValues values2 = new ContentValues();  
			values2.put("name", "xiaosan");  
			int i2 = db4.update("SuperFriendsBean", values2, "id=?", new String[]{"1"});*/
			
			DatabaseHelperForChattingMap dbHelper7 = new DatabaseHelperForChattingMap(TestDatabasePOIActivity3.this, "db", 2);  
			SQLiteDatabase db7 = dbHelper7.getReadableDatabase();  
			//创建游标对象  
			String name = "name";
			String mSelfCode = "mSelfCode";
			
			Cursor cursor2 = db7.query("SuperFriendsBean", new String[]{"id","username","uid", "avatar"}, null, null, null, null, null, null);  
			//利用游标遍历所有数据对象  
			while(cursor2.moveToNext()){  
				int id = cursor2.getInt(cursor2.getColumnIndex("id"));
				String username =  cursor2.getString(cursor2.getColumnIndex("username"));
				String avatar =  cursor2.getString(cursor2.getColumnIndex("avatar"));
				String uid =  cursor2.getString(cursor2.getColumnIndex("uid"));
				//日志打印输出  
				Log.i(SWORD,"query-->"+username);  
			}  
			break;  
			//查询信息  
		case R.id.query:  
			DatabaseHelperForChattingMap dbHelper5 = new DatabaseHelperForChattingMap(TestDatabasePOIActivity3.this, "db", 2);  
			SQLiteDatabase db5 = dbHelper5.getReadableDatabase();  
			//创建游标对象  
			Cursor cursor = db5.query("SuperFriendsBean", new String[]{"id","username","uid", "avatar"}, null, null, null, null, null, null);  
			//利用游标遍历所有数据对象  
			while(cursor.moveToNext()){  
				int id = cursor.getInt(cursor.getColumnIndex("id"));
				String username =  cursor.getString(cursor.getColumnIndex("username"));
				String avatar =  cursor.getString(cursor.getColumnIndex("avatar"));
				String uid =  cursor.getString(cursor.getColumnIndex("uid"));
				//日志打印输出  
				Log.i(SWORD,"query-->"+username);  
			}  
			break;  
			//删除记录  
		case R.id.delete:  
			DatabaseHelperForChattingMap dbHelper6 = new DatabaseHelperForChattingMap(TestDatabasePOIActivity3.this,"db", 2);  
			SQLiteDatabase db6 = dbHelper6.getWritableDatabase();  
			db6.delete("SuperFriendsBean", "id=?", new String[]{"1"});  
			break;  
		default:  
			Log.i(SWORD,"error");  
			break;  
		}  
	}  
}  